package com.example.myspringai.controller;

import com.example.myspringai.myredis.RedisChatMemory;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@RestController
@RequestMapping("/chat-memory")
public class ChatMemoryController {

    private final ChatClient chatClient;

    public ChatMemoryController(ChatModel chatModel) {
        this.chatClient = ChatClient
                .builder(chatModel)
                .defaultSystem("你是一个旅游规划师，请根据用户的需求提供旅游规划建议。")
                .defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory())) //系统内存存储
              //  .defaultAdvisors(new MessageChatMemoryAdvisor(new RedisChatMemory("127.0.0.1", 6379, null))) //redis内存存储
                .build();
    }

    /**
     * 测试内存存储
     * @param prompt
     * @param chatId
     * @param response
     * @return
     */
    @GetMapping("/in-memory")
    public Flux<String> memory(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        return chatClient.prompt(prompt)
                .advisors(
                        a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)
                )
                .stream()
                .content();
    }

    /**
     * 测试redis存储（如果使用请修改memoryIndex.html页面接口）
     * @param prompt
     * @param chatId
     * @param response
     * @return
     */
    @GetMapping("/in-redis")
    public Flux<String> redis(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");

        return chatClient.prompt(prompt)
                .advisors(
                        a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)
                )
                .stream()
                .content();
    }

}
